/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.model.events;

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author  Schalk
 * @version
 */
public class ActivateEvent extends HttpServlet {
    
    public String DRIVER, URL, USER, PASS;
    public String app_root,messageSuccess,messageFail,event_id,series,comp_logo,comp_logo_alt,comp_name,lb_comp_logo,lb_comp_logo_alt,
	town,region,comp_date,comp_time,entry_form,entry_form_alt;
    
    /* The below would need to be set via a properties or XML 
     * configuration file per company dependant on which company 
     * are accessing these methods.
     * 
     * There may be a better way to do this and this should be
     * investigated.
    */ 
	public void init() throws ServletException {
    ServletContext context = getServletContext();
    DRIVER = context.getInitParameter("driver");
    URL = context.getInitParameter("mc_dburl");
    USER = context.getInitParameter("mc_dbuser");
    PASS = context.getInitParameter("mc_dbpass");
    }
    
    public void processRequest (HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
	response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	Connection con = null;
	messageSuccess = "Update of event completed successfuly.";
	messageFail = "Update of event failed.";

	try {	                
        Class.forName(DRIVER);
		con = DriverManager.getConnection(URL,USER,PASS);
		Statement stmt = con.createStatement();               
                
		String gatherData = 
                "SELECT * FROM ab_events_pending " +
                "WHERE id= '" + request.getParameter("event_id") + "'";
                
                ResultSet rs = stmt.executeQuery(gatherData);
                boolean hasData = false;
                
                while(rs.next()) {
                	hasData = true;
                 
	                event_id = rs.getString(2);
	                series = rs.getString(3);
	                comp_logo = rs.getString(4);
	                comp_logo_alt = rs.getString(5);
	                comp_name = rs.getString(6);
	                lb_comp_logo = rs.getString(7);                
	                lb_comp_logo_alt = rs.getString(8);
	                town = rs.getString(9);
	                region = rs.getString(10);
	                comp_date = rs.getString(11);
	                comp_time = rs.getString(12);
	                entry_form = rs.getString(13);
	                entry_form_alt = rs.getString(14);       
                    
                }
                if (hasData == false) {
                	response.sendRedirect("?message=");                	
                }
                
                String insertData = 
                "INSERT INTO ab_events " + 
                " (event_id,series,comp_logo,comp_logo_alt,comp_name,lb_comp_logo," +
                "lb_comp_logo_alt,town,region,comp_date,comp_time,entry_form,entry_form_alt)" + 
                " VALUES('" + "')";          
              
                int result = stmt.executeUpdate(insertData);
                
                String deleteData =
                "DELETE FROM ab_events_pending " +
                "WHERE id= '" + event_id + "'";
                
                int deleteResult = stmt.executeUpdate(deleteData);
              
                app_root = request.getContextPath();
				response.sendRedirect(app_root + "/application/application_messages/process_result.jsp?message=" + messageSuccess);	
        
} catch (SQLException ex) {
	out.println("\nERROR:------ SQLException -----\n");
        while (ex != null) {
            out.println("Message: " + ex.getMessage() + "<br>");
            out.println("SQLState: " + ex.getSQLState() + "<br>");
            out.println("ErrorCode :" + ex.getErrorCode());
            ex = ex.getNextException();
        }
    } catch (Exception e) {
        e.printStackTrace();        
} finally {
    try {	
    if (con != null)
        con.close();
	} catch (SQLException ex) {
            out.println("\nERROR:------ SQLException -----\n");
            out.println("Message: " + ex.getMessage());
            out.println("SQLState: " + ex.getSQLState());
            out.println("ErrorCode :" + ex.getErrorCode());
        }
            }
    }
    
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }    
   
}
